package cn.tedu.egweibo.controller;

import cn.tedu.egweibo.base.respont.JsonResult;
import cn.tedu.egweibo.base.respont.StatusCode;
import cn.tedu.egweibo.mapper.WeiboMapper;
import cn.tedu.egweibo.pojo.dto.WeiboAddParam;
import cn.tedu.egweibo.pojo.entity.Weibo;
import cn.tedu.egweibo.pojo.vo.UserVO;
import cn.tedu.egweibo.pojo.vo.WeiboDetailVO;
import cn.tedu.egweibo.pojo.vo.WeiboIndexVO;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;
@Validated
@Slf4j
@RestController
@RequestMapping("/v1/weibo/")
@Api(tags = "02.微博管理系统")
public class WeiboController {
    @Autowired
    private WeiboMapper weiboMapper;
    @PostMapping("insert")
    @ApiOperation("发布微博")
    @ApiOperationSupport(order = 100)
    public JsonResult insertWeibo(@RequestBody WeiboAddParam weiboAddParam, @ApiIgnore HttpSession session){
        log.debug("weiboAddParam{}", weiboAddParam);
        UserVO userVO = (UserVO) session.getAttribute("user");
        Weibo weibo = new Weibo();
        if (userVO == null){
            return new JsonResult(StatusCode.NOT_LOGIN);
        }
        BeanUtils.copyProperties(weiboAddParam,weibo);
        weibo.setCreateTime(new Date());
        weibo.setUserId(userVO.getId());
        weiboMapper.insert(weibo);
        return JsonResult.ok();
    }
    @GetMapping("selectIndex")
    @ApiOperation("微博首页列表")
    @ApiOperationSupport(order = 200)
    public JsonResult selectIndex(){
        List<WeiboIndexVO> list = weiboMapper.selectIndex();
        return JsonResult.ok(list);
    }
    @GetMapping("selectById")
    @ApiOperation("微博详情页")
    @ApiOperationSupport(order = 300)
    public JsonResult selectById(@Range(min = 100, max = 500, message = "微博编号必须在100-500之间")
                                     @RequestParam Integer id){
        WeiboDetailVO weiboDetailVO= weiboMapper.selectById(id);
        return JsonResult.ok(weiboDetailVO);
    }
}

